package UILayer;

import java.util.ArrayList;
import java.util.Scanner;

import CtrLayer.SalgCtr;
import ModelLayer.Salg;

public class SalgUI {
	
	private SalgCtr sCtr;
	
	//Salg Nr
	private int salgNr;
	
	/**
	 * Constructor for the SalgUI
	 * */
	public SalgUI()
	{
		sCtr = new SalgCtr();
	}
	
	/**
	 * 
	 * */
	public void startSalgUI()
	{
		boolean start = false;
		while(!start)
		{
			Scanner input = new Scanner(System.in);
			System.out.println("1. Start new Salg");
			System.out.println("2. Add delsalg to Salg");
			System.out.println("3. Add discount to salg");
			System.out.println("4. Add Kunde to salg");
			System.out.println("5. Get price");
			System.out.println("6. Set Paid status");
			System.out.println("7. End salg");
			System.out.println("8. Print all salgs Nr");
			
			int choice = input.nextInt();
			switch(choice){
				case 1:
					startNewSale();
					break;
				case 2:
					addDelSalgToSalg();
					break;
				case 3:
					addDiscountToSalg();
					break;
				case 4:
					addKundeToSalg();
					break;
				case 5:
					getTotalPrice();
					break;
				case 6:
					setPaidStatus();
					break;
				case 7:
					endSalg();
					break;
				case 8:
					printAllSalgNr();
					break;
					default:
						System.out.println("Sale is ended!");
						start = true;
						break;
			}
		}
	}
	/**
	 * Start new sale
	 * Prompt user for phone No.
	 * 
	 * */
	public void startNewSale()
	{
		Scanner input = new Scanner(System.in);
		System.out.println("Enter phone no.: ");
		String phone = input.next();
		
		sCtr.startNewSale(phone);
	}
	
	/**
	 * Add a DelSalg to the sale.
	 * Prompt user for artikelNr and amount.
	 * 
	 * */
	public void addDelSalgToSalg()
	{
		Scanner input = new Scanner(System.in);
		System.out.println("Enter artikel Nr.: ");
		int aN = input.nextInt();
		input.nextLine();
		System.out.println("Enter amount: ");
		int a = input.nextInt();
		input.nextLine();
		System.out.println("Enter salgNr: ");
		int sN = input.nextInt();
		
		salgNr = sN;
		
		sCtr.addDelSalgToSale(aN, a, sN);
		System.out.println("DelSalg added!");
		System.out.println("Currrent price: Kr. " + sCtr.getPriceOfSalg(sN) + ",-");
	}
	
	/**
	 * Add discount to salg
	 * The discount is made in percent
	 * Thats why its an int.
	 * */
	public void addDiscountToSalg()
	{
		Scanner input = new Scanner(System.in);
		System.out.println("Enter discount: ");
		int dis = input.nextInt();
		input.nextLine();
		
		sCtr.addDiscountToSalg(dis);
		System.out.println("Discount added!");
		System.out.println("Current price: Kr. " + sCtr.getPriceOfSalg(salgNr) + ",-");
	}
	
	/**
	 * 
	 * */
	public void getTotalPrice()
	{
		
		System.out.println("Price: Kr. " + sCtr.getPriceOfSalg(salgNr)+ ",- ");
		System.out.println("Paid status: " + sCtr.getBetaltStatus());
	}
	
	/**
	 * Set the paid status
	 * 
	 * */
	public void setPaidStatus()
	{
		sCtr.setBetaltStatus();
		System.out.println(sCtr.getBetaltStatus());
	}
	/**
	 * Add kunde to salg*/
	public void addKundeToSalg()
	{
		sCtr.addKundeToSalg();
	}
	/**
	 * End salg.
	 * Check paid status if true finish sale.
	 * */
	public void endSalg()
	{
		if(sCtr.getBetaltStatus() == true){
			System.out.println("Price: Kr. " + sCtr.getPriceOfSalg(salgNr) + ",- ");
			System.out.println("Thank you, please come agian!");
		}else{
			System.out.println("Paid status: " + sCtr.getBetaltStatus());
			System.out.println("Get payment before they run!");
		}
	}
	/**
	 * Print all salg
	 * */
	public void printAllSalgNr()
	{
		ArrayList<Salg> salgList = sCtr.getAllSalg();
		for(Salg x: salgList){
			System.out.println("Salg Nr: " + x.getSalgsNr());
			System.out.println("Delsagl: " + x.getAllDelSalg().size());
			System.out.println("Price: " + x.getTotalPrice() + ",-");
		}
	}
}